package com.managertrade.dao.sqlite.repository;
import com.managertrade.dao.sqlite.entity.KlineWithIndicatorsEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface KlineWithIndicatorsRepository extends JpaRepository<KlineWithIndicatorsEntity, String> {
    /**
     * 根据 uniqueId 批量查询已存在的 Kline
     * @param uniqueIds uniqueId 列表
     * @return 已存在的实体列表
     */
    List<KlineWithIndicatorsEntity> findAllByUniqueIdIn(List<String> uniqueIds);


    /**
     * 获取指定币种与周期的最新一条 K 线数据
     */
    KlineWithIndicatorsEntity findTopBySymbolAndPeriodOrderByTimestampDesc(String symbol, String period);


    // JPA分页查询方法
    // JPA分页查询方法
    List<KlineWithIndicatorsEntity> findBySymbolAndPeriod(String symbol, String period, Pageable pageable);


    // 使用 native query 取最新 N 条
    @Query(value = "SELECT * FROM kline_with_indicators WHERE symbol=:symbol AND period=:period ORDER BY timestamp DESC LIMIT :limit", nativeQuery = true)
    List<KlineWithIndicatorsEntity> findLatestBySymbolAndPeriod(
            @Param("symbol") String symbol,
            @Param("period") String period,
            @Param("limit") int limit
    );

}

